package com.github.data_manage.dao

import com.github.data_manage.domain.po.PersonEducationExperience
import com.github.data_manage.domain.po.Persons
import org.springframework.data.jpa.repository.Modifying
import org.springframework.data.jpa.repository.Query
import org.springframework.data.repository.query.Param
import java.time.LocalDate

/**
 *@author CHN
 *@since 2024-08-07 22:07
 */
interface PersonEducationExperienceDao : BaseDao<PersonEducationExperience> {

    fun removeAllByPersonIdEquals(personId: Int)

    @Query("select p from PersonEducationExperience p where p.personId = :personId order by p.startDate desc nulls last ")
    fun findAllByPersonIdEquals(personId: Int): List<PersonEducationExperience>



//    and ((p.endDate between :startDate and :endDate or p.startDate >= :startDate) or (p.endDate is null or p.endDate >= :startDate))
    @Query(
        """
        select p from PersonEducationExperience p where p.schoolId = :schoolId and p.personId != :personId
    """
    )
    fun findSchoolMates(
        @Param("personId") personId: Int,
        @Param("schoolId") schoolId: Int,
//        @Param("startDate") startDate: LocalDate?,
//        @Param("endDate") endDate: LocalDate?
    ): List<PersonEducationExperience>

    @Modifying
    @Query("update PersonEducationExperience set schoolId = :mainSchoolId,schoolName= :mainSchoolName where schoolId = :minorSchoolId")
    fun mergeSchool(
        @Param("mainSchoolId") mainSchoolId: Int,
        @Param("mainSchoolName") mainSchoolName: String,
        @Param("minorSchoolId") minorSchoolId: Int
    )

}
